package com.google.code.dnaep.jdo;

import java.util.logging.Logger;

import javax.jdo.JDOHelper;
import javax.jdo.listener.AttachLifecycleListener;
import javax.jdo.listener.ClearLifecycleListener;
import javax.jdo.listener.CreateLifecycleListener;
import javax.jdo.listener.DeleteLifecycleListener;
import javax.jdo.listener.DetachLifecycleListener;
import javax.jdo.listener.DirtyLifecycleListener;
import javax.jdo.listener.InstanceLifecycleEvent;
import javax.jdo.listener.LoadLifecycleListener;
import javax.jdo.listener.StoreLifecycleListener;

public final class PMF {
	protected static final Logger log = Logger.getLogger(PMF.class.getName()); 
	
    private static final javax.jdo.PersistenceManagerFactory pmfInstance =
        JDOHelper.getPersistenceManagerFactory("transactions-optional");

    static {
    	pmfInstance.addInstanceLifecycleListener(new MyAttachListener(), null);
    	pmfInstance.addInstanceLifecycleListener(new MyClearListener(), null);
    	pmfInstance.addInstanceLifecycleListener(new MyDeleteListener(), null);
    	pmfInstance.addInstanceLifecycleListener(new MyDetachListener(), null);
    	pmfInstance.addInstanceLifecycleListener(new MyDirtyListener(), null);
    	pmfInstance.addInstanceLifecycleListener(new MyLoadListener(), null);
    	pmfInstance.addInstanceLifecycleListener(new MyStoreListener(), null);
    }
    
    private PMF() {}

    public static javax.jdo.PersistenceManagerFactory get() {
        return pmfInstance;
    }


    
    public static class MyAttachListener implements AttachLifecycleListener {

		@Override
        public void preAttach(InstanceLifecycleEvent event) {
			log.info("preAttach: " + event.getPersistentInstance());
        }

		@Override
        public void postAttach(InstanceLifecycleEvent event) {
			log.info("postAttach: " + event.getPersistentInstance());
        }
    }

    public static class MyClearListener implements ClearLifecycleListener {

		@Override
        public void preClear(InstanceLifecycleEvent event) {
			log.info("preClear: " + event.getPersistentInstance());
        }

		@Override
        public void postClear(InstanceLifecycleEvent event) {
			log.info("postClear: " + event.getPersistentInstance());
        }
    }

    public static class MyCreateListener implements CreateLifecycleListener {

		@Override
        public void postCreate(InstanceLifecycleEvent event) {
			log.info("postCreate: " + event.getPersistentInstance());
        }
    }

    public static class MyDeleteListener implements DeleteLifecycleListener {

		@Override
        public void preDelete(InstanceLifecycleEvent event) {
			log.info("preDelete: " + event.getPersistentInstance());
        }

		@Override
        public void postDelete(InstanceLifecycleEvent event) {
			log.info("postDelete: " + event.getPersistentInstance());
        }
    }

    public static class MyDetachListener implements DetachLifecycleListener {

		@Override
        public void preDetach(InstanceLifecycleEvent event) {
			log.info("preDetach: " + event.getPersistentInstance());
        }

		@Override
        public void postDetach(InstanceLifecycleEvent event) {
			log.info("postDetach: " + event.getPersistentInstance());
        }
    }

    public static class MyDirtyListener implements DirtyLifecycleListener {

		@Override
        public void preDirty(InstanceLifecycleEvent event) {
			log.info("preDirty: " + event.getPersistentInstance());
        }

		@Override
        public void postDirty(InstanceLifecycleEvent event) {
			log.info("postDirty: " + event.getPersistentInstance());
        }
    }

    public static class MyLoadListener implements LoadLifecycleListener {

		@Override
        public void postLoad(InstanceLifecycleEvent event) {
			log.info("postLoad: " + event.getPersistentInstance());
        }
    }

    public static class MyStoreListener implements StoreLifecycleListener {

		@Override
        public void preStore(InstanceLifecycleEvent event) {
			log.info("preStore: " + event.getPersistentInstance());
        }

		@Override
        public void postStore(InstanceLifecycleEvent event) {
			log.info("postStore: " + event.getPersistentInstance());
        }
    }

}
